//
//  RegisterVC.m
//  dianfan
//
//  Created by Ye Jin on 15/11/9.
//  Copyright © 2015年 Ye Jin. All rights reserved.
//

#import "RegisterVC.h"
#import <BmobSDK/Bmob.h>
#import "MBProgressHUD.h"

@interface RegisterVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *account;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UITextField *securePD;

@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.account.delegate = self;
    self.account.returnKeyType = UIReturnKeyDone;
    self.passWord.delegate = self;
    self.passWord.returnKeyType = UIReturnKeyDone;
    [self.passWord setSecureTextEntry:YES];
    self.securePD.delegate = self;
    self.securePD.returnKeyType = UIReturnKeyDone;
    [self.securePD setSecureTextEntry:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)registerBtN:(id)sender {
    if ([self.account.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"用户名不能为空" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if ([self.passWord.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"密码不能为空" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if ([self.passWord.text isEqualToString: self.securePD.text])
    {
        BmobUser *bUser = [[BmobUser alloc] init];
        [bUser setUserName:self.account.text];
        [bUser setPassword:self.passWord.text];
        
        //转菊花
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
        hud.labelText=@"loading";
        
        [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
            if (isSuccessful) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"注册成功" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            else if (error)
            {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@",[[error userInfo] objectForKey:@"error"]] message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"unknow error" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
        self.account.text = @"";
        self.passWord.text = @"";
        self.securePD.text = @"";
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"两次输入的密码不一样" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        self.passWord.text = @"";
        self.securePD.text = @"";
    }
}

- (IBAction)cancelBtn:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //实现键盘往下收
    [textField resignFirstResponder];
    return YES;
}

//点击屏幕空白处去掉键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.account resignFirstResponder];
    [self.passWord resignFirstResponder];
    [self.securePD resignFirstResponder];
}

@end
